//
//  YSLScrollMenuView.m
//  YSLContainerViewController
//
//  Created by yamaguchi on 2015/03/03.
//  Copyright (c) 2015年 h.yamaguchi. All rights reserved.
//

#import "YSLScrollMenuView.h"
#import <Masonry/Masonry.h>

//static const CGFloat kYSLScrollMenuViewWidth  = 60;
//static const CGFloat kYSLScrollMenuViewMargin = 5;
//static const CGFloat kYSLIndicatorHeight = 0;

@interface YSLScrollMenuView (){
    CGFloat kYSLScrollMenuViewWidth;
    CGFloat kYSLScrollMenuViewMargin;
    CGFloat kYSLIndicatorHeight;
}

@property (nonatomic, strong) UIView *indicatorView;
@property (nonatomic, assign) CGAffineTransform baseTransform;

@end

@implementation YSLScrollMenuView


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // default
        kYSLScrollMenuViewWidth = 60;
        kYSLScrollMenuViewMargin = 5;
        _viewbackgroudColor = [UIColor whiteColor];
        _itemfont = [UIFont systemFontOfSize:16];
        _itemTitleColor = [UIColor colorWithRed:0.866667 green:0.866667 blue:0.866667 alpha:1.0];
        _itemSelectedTitleColor = [UIColor colorWithRed:0.333333 green:0.333333 blue:0.333333 alpha:1.0];
        _itemIndicatorColor = [UIColor colorWithRed:0.168627 green:0.498039 blue:0.839216 alpha:1.0];
        
        self.backgroundColor = _viewbackgroudColor;
        _scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
        _scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_scrollView];
    }
    return self;
}

#pragma mark -- Setter

- (void)setViewbackgroudColor:(UIColor *)viewbackgroudColor
{
    if (!viewbackgroudColor) { return; }
    _viewbackgroudColor = viewbackgroudColor;
    self.backgroundColor = viewbackgroudColor;
}

- (void)setItemfont:(UIFont *)itemfont
{
    if (!itemfont) { return; }
    _itemfont = itemfont;
    for (UILabel *label in _itemTitleArray) {
        label.font = itemfont;
    }
}

- (void)setItemTitleColor:(UIColor *)itemTitleColor
{
    if (!itemTitleColor) { return; }
    _itemTitleColor = itemTitleColor;
    for (UILabel *label in _itemTitleArray) {
        label.textColor = itemTitleColor;
    }
}

- (void)setItemIndicatorColor:(UIColor *)itemIndicatorColor
{
    if (!itemIndicatorColor) { return; }
    _itemIndicatorColor = itemIndicatorColor;
    _indicatorView.backgroundColor = itemIndicatorColor;
}

- (void)setBNeedIndicator:(BOOL)bNeedIndicator
{
    _bNeedIndicator = bNeedIndicator;
    kYSLIndicatorHeight = bNeedIndicator ? 2 : 0;
}

- (void)setItemTitleArray:(NSArray *)itemTitleArray
{
    if (_itemTitleArray != itemTitleArray) {
        _itemTitleArray = itemTitleArray;
        NSMutableArray<UIView *> *views = [NSMutableArray array];
        CGFloat wid = ScreenWidth / itemTitleArray.count;
        if (wid > kYSLScrollMenuViewWidth) {
            kYSLScrollMenuViewWidth = wid;
            kYSLScrollMenuViewMargin = 0;
        }
        for (int i = 0; i < itemTitleArray.count; i++) {
            CGRect frame = CGRectMake(0, 0, kYSLScrollMenuViewWidth, CGRectGetHeight(self.frame));
            UILabel *itemView = [[UILabel alloc] initWithFrame:frame];
            [self.scrollView addSubview:itemView];
            itemView.tag = i;
            itemView.text = itemTitleArray[i];
            itemView.userInteractionEnabled = YES;
            itemView.backgroundColor = [UIColor clearColor];
            itemView.textAlignment = NSTextAlignmentCenter;
            itemView.font = self.itemfont;
            itemView.textColor = _itemTitleColor;
//            itemView.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
            [views addObject:itemView];
            
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(itemViewTapAction:)];
            [itemView addGestureRecognizer:tapGesture];
        }
        self.baseTransform = CGAffineTransformScale(views[views.count - 1].transform, 1, 1);
        self.itemViewArray = [NSArray arrayWithArray:views];
        
        // indicator
        _indicatorView = [[UIView alloc]init];
        _indicatorView.frame = CGRectMake(kYSLScrollMenuViewMargin, _scrollView.frame.size.height - kYSLIndicatorHeight, kYSLScrollMenuViewWidth, kYSLIndicatorHeight);
        
        UIView *realIndicator = [UIView new];
        realIndicator.backgroundColor = self.itemIndicatorColor;
        [_indicatorView addSubview:realIndicator];
        [realIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_indicatorView);
            make.top.bottom.mas_equalTo(0);
            if (kYSLScrollMenuViewWidth > 70) {
                make.width.mas_equalTo(70);
            }
            else{
                make.left.right.mas_equalTo(0);
            }
        }];
        
//        _indicatorView.backgroundColor = self.itemIndicatorColor;
        [_scrollView addSubview:_indicatorView];
    }
}

#pragma mark -- public

- (void)setIndicatorIndex:(NSUInteger)index
{
    CGFloat indicatorX = (kYSLScrollMenuViewMargin + kYSLScrollMenuViewWidth) * index + kYSLScrollMenuViewMargin;
    _indicatorView.left = indicatorX;
}

- (void)setIndicatorViewFrameWithRatio:(CGFloat)ratio isNextItem:(BOOL)isNextItem toIndex:(NSInteger)toIndex
{
    CGFloat indicatorX = 0.0;
    if (isNextItem) {
        indicatorX = ((kYSLScrollMenuViewMargin + kYSLScrollMenuViewWidth) * ratio ) + (toIndex * kYSLScrollMenuViewWidth) + ((toIndex + 1) * kYSLScrollMenuViewMargin);
    } else {
        indicatorX =  ((kYSLScrollMenuViewMargin + kYSLScrollMenuViewWidth) * (1 - ratio) ) + (toIndex * kYSLScrollMenuViewWidth) + ((toIndex + 1) * kYSLScrollMenuViewMargin);
    }
    
    if (indicatorX < kYSLScrollMenuViewMargin || indicatorX > self.scrollView.contentSize.width - (kYSLScrollMenuViewMargin + kYSLScrollMenuViewWidth)) {
        return;
    }
    _indicatorView.frame = CGRectMake(indicatorX, _scrollView.frame.size.height - kYSLIndicatorHeight, kYSLScrollMenuViewWidth, kYSLIndicatorHeight);
    //  NSLog(@"retio : %f",_indicatorView.frame.origin.x);
    
    //如果是点击，因为马上变色了，就不需要渐变
    if(self.tapToScroll){
        return;
    }
    //更改颜色渐变
    CGFloat a,r,g,b,aa = 0.0,rr = 0.0,gg = 0.0,bb = 0.0;
    UILabel *normalLabel = self.itemViewArray[isNextItem ? toIndex + 1 : toIndex];
    UILabel *highLabel = self.itemViewArray[isNextItem ? toIndex : toIndex + 1];
    if([self.itemTitleColor getRed:&r green:&g blue:&b alpha:&a] &&
       [self.itemSelectedTitleColor getRed:&rr green:&gg blue:&bb alpha:&aa]){
        CGFloat _r = (rr - r) * ratio;
        CGFloat _g = (gg - g) * ratio;
        CGFloat _b = (bb - b) * ratio;
        CGFloat _a = (aa - a) * ratio;
        normalLabel.textColor = [UIColor colorWithRed:r + _r green:g + _g blue:b + _b alpha:a + _a];
        highLabel.textColor = [UIColor colorWithRed:rr - _r green:gg - _g blue:bb - _b alpha:aa - _a];
    }
    //更改文字大小
    normalLabel.transform = CGAffineTransformScale(self.baseTransform, 1 + 0.1 * ratio, 1 + 0.1 * ratio);
    highLabel.transform = CGAffineTransformScale(self.baseTransform, 1.1 - 0.1 * ratio, 1.1 - 0.1 * ratio);
}

- (void)setItemTextColor:(UIColor *)itemTextColor
    seletedItemTextColor:(UIColor *)selectedItemTextColor
            currentIndex:(NSInteger)currentIndex
{
    if (itemTextColor) { _itemTitleColor = itemTextColor; }
    if (selectedItemTextColor) { _itemSelectedTitleColor = selectedItemTextColor; }
    
    for (int i = 0; i < self.itemViewArray.count; i++) {
        UILabel *label = self.itemViewArray[i];
        label.textColor = i == currentIndex ? _itemSelectedTitleColor : _itemTitleColor;
        label.transform = CGAffineTransformScale(self.baseTransform, i == currentIndex ? 1.1 : 1, i == currentIndex ? 1.1 : 1);
    }
}

#pragma mark -- private

// menu shadow
- (void)setShadowView
{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, self.frame.size.height - 0.5, CGRectGetWidth(self.frame), 0.5);
    view.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:view];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat x = kYSLScrollMenuViewMargin;
    for (NSUInteger i = 0; i < self.itemViewArray.count; i++) {
        CGFloat width = kYSLScrollMenuViewWidth;
        UIView *itemView = self.itemViewArray[i];
        itemView.frame = CGRectMake(x, 0, width, self.scrollView.frame.size.height);
        x += width + kYSLScrollMenuViewMargin;
    }
    self.scrollView.contentSize = CGSizeMake(x, self.scrollView.frame.size.height);
    
    CGRect frame = self.scrollView.frame;
    if (self.frame.size.width > x) {
        frame.origin.x = (self.frame.size.width - x) / 2;
        frame.size.width = x;
    } else {
        frame.origin.x = 0;
        frame.size.width = self.frame.size.width;
    }
    self.scrollView.frame = frame;
}

#pragma mark -- Selector --------------------------------------- //
- (void)itemViewTapAction:(UITapGestureRecognizer *)Recongnizer
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(scrollMenuViewSelectedIndex:)]) {
        self.tapToScroll = YES;
        [self.delegate scrollMenuViewSelectedIndex:[(UIGestureRecognizer*) Recongnizer view].tag];
    }
}

@end
